package org.distributed.transaction.core.aspect;

import java.sql.Connection;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.distributed.transaction.core.callback.ConnectionCallback;
import org.distributed.transaction.core.interceptor.DataSourceInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 获取数据库连接时候的切面
 */
@Aspect
@Component
public class DataSourceAspect {
	
	@Autowired
	private DataSourceInterceptor dataSourceInterceptor;

	@Around("execution(* javax.sql.DataSource.getConnection(..))")
    public Object around(ProceedingJoinPoint point) throws Throwable {
		return dataSourceInterceptor.getConnection(new ConnectionCallback() {
			@Override
			public Connection callback() throws Throwable {
				return (Connection) point.proceed();
			}
		});
    }
}
